/*
 * =====================================================================================
 *
 *       Filename:  debug.h
 *
 *    Description:  debug api
 *
 *        Version:  1.0
 *        Created:  2013年01月30日 09时59分17秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  jianxi sun (jianxi), ycsunjane@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */

#ifndef __DEBUG__
#define __DEBUG__
/*
 * DEBUG_PRINTF
 * */
/*
 * CONFIG: make menuconfig define CONFIG
 * */
#define DEBUG_PRINTF(CONFIG,fmt,arg...) 		\
do{ 							\
#ifdef CONFIG 						\
printf("[%28s][%4d]:"fmt,__FUNCTION__,__LINE__,##arg);  \
#else 							\
NULL; 							\
#endif 							\
}while(0)						\


/*
 * HEX_DUMP
 * BIG_ENDIA:  
 * lth: 0xf0 => f0  htl: 0xf0 => 0f
 * LITTLE_ENDIA:
 * lth:0xf0 => 0f htl:0xf0 => f0
 * */
static void hex_dump_lth(uint8_t *data_ptr,uint32_t len)
{
	int j,i=0;
	for(j=0;j<len;j++){
		printf("%02x ",*(data_ptr+j));
		if(++i==8){
			i=0;printf("\n");
		}
	}
	printf("\n");
}
static void hex_dump_htl(uint8_t *data_ptr,uint32_t len)
{
	int j,i=0;
	for(j=len-1;j>=0;j--){
		printf("%02x ",*(data_ptr+j));
		if(++i==8){
			i=0;printf("\n");
		}
	}
	printf("\n");
}

/*
 * BIN_DUMP:
 *
 * */
bin_dump_lth(uint8_t *data,uint32_t len)
{
	int i,j;
	uint8_t tmp;
	for(i=0;i<len-1;i++){
		tmp = *(data+i);
		for(j=7;j>=0;j--){
			printf("%d",((tmp>>j) & 0x1));
		}
	}
	printf("\n");
}
bin_dump_htl(uint8_t *data,uint32_t len)
{
	int i,j;
	uint8_t tmp;
	for(i=--len;i>=0;i--){
		tmp = *(data+i);
		for(j=7;j>=0;j--){
			printf("%d",((tmp>>j) & 0x1));
		}
		printf (" ");
	}
	printf("\n");
}
/*
 * ASSERT
 * */
static void __Assert(char * strFile,unsigned uLine)
{
	printf("\nAssertion failed:%s,line %u \n",strFile,uLine);
	abort();
}

#ifdef CONFIG_DEBUG_MODULE
#define ZXMX_MODULE_DEBUG(modules,fmt,arg...) \
do{ \
if(strequ(modules,CONFIG_DEBUG_MODULE)) \
printf("%s:""[%28s][%4d]:" fmt ,CONFIG_DEBUG_MODULE, __FUNCTION__, __LINE__, ##arg); \
}while(0)
#else
#define ZXMX_MODULE_DEBUG(modules,fmt,arg...) NULL
#endif

/*
 * strcmp - compare two string
 * return: 
 *       if str1 equal str2 then return 1,else return 0 
 * */
static inline int strequ(char *str1,char *str2)
{
	int i=0;
	do{
		if(str1[i] == str2[i])
			continue;
		else
			return 0;
	}while(str1[i++] != '\0');
	return 1;
}


#define ASSERT(f) \
	if(f) \
		NULL; \
	else \
		__Assert(__FILE__,__LINE__)

#endif /*__DEBUG__ */
